﻿using Newtonsoft.Json;
using System;
using System.Net;
using System.Text;

namespace TYBase
{
    public static class StringExtend
    {

        public static string EmptyAs(this string str, string result)
        {
            if (str.Null())
                return result;
            return str;
        }
        public static int AsInt(this string str, int ret = 0)
        {
            if (str.Null())
                return ret;
            if (int.TryParse(str, out int data))
            {
                return data;
            }
            return ret;
        }

        public static long AsLong(this string str, long ret = 0)
        {
            if (str.Null())
                return ret;
            if (long.TryParse(str, out long data))
            {
                return data;
            }
            return ret;
        }

        public static double AsDouble(this string str, double ret = 0)
        {
            if (str.Null())
                return ret;
            if (double.TryParse(str, out double data))
            {
                return data;
            }
            return ret;
        }

        public static bool Null(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        public static bool NotNull(this string str)
        {
            return !string.IsNullOrEmpty(str);
        }

        public static bool NotNull(this string str, string NoString)
        {
            return !string.IsNullOrEmpty(str) && str.ToUpper() != NoString.ToUpper();
        }

        public static byte[] AsBytes(this string str)
        {
            return Encoding.Default.GetBytes(str);
        }



        public static byte[] AsBytes(this string str, Encoding encoding)
        {
            return encoding.GetBytes(str);
        }

        public static DateTime AsTime(this string dateString, DateTime dtDefault, string format = "yyyy-MM-dd HH:mm:ss")
        {
            try
            {
                if (dateString.Null())
                    return dtDefault;
                return DateTime.ParseExact(dateString, format, System.Globalization.CultureInfo.CurrentCulture);
            }
            catch (Exception ex)
            {
                return dtDefault;
            }
        }
        public static DateTime? AsTime(this string dateString, DateTime? dtDefault, string format = "yyyy-MM-dd HH:mm:ss")
        {
            try
            {
                if (dateString.Null())
                    return dtDefault;
                return DateTime.ParseExact(dateString, format, System.Globalization.CultureInfo.CurrentCulture);
            }
            catch (Exception ex)
            {
                return dtDefault;
            }
        }
        public static T AsModel<T>(this string str)
        {
            try
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str,new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });
            }
            catch (Exception ex)
            {
                Log.E($"JsonToModel{str}-{ex.Info()}");

                return default(T);
            }
        }

        public static bool IsIP(this string str)
        {
            if (IPAddress.TryParse(str, out IPAddress ip))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static string Left(this string str, int leftCount)
        {
            if (str.Null())
            {
                return String.Empty;
            }
            if (str.Length <= leftCount)
            {
                return str;
            }
            return str.Substring(0, leftCount);
        }

        public static string Right(this string str, int leftCount)
        {
            if (str.Null())
            {
                return String.Empty;
            }
            if (str.Length <= leftCount)
            {
                return str;
            }
            return str.Substring(str.Length - leftCount, leftCount);
        }

        public static string[] Split(this string str, string splitChar = "\r\n")
        {
            string[] stringSeparators = new string[] { splitChar };
            string[] lines = str.Split(stringSeparators, StringSplitOptions.None);
            return lines;
        }

        public static string NullAs(this string str, string splitChar = "")
        {
            if (str.Null())
                return splitChar;
            return str;
        }

        public static string AsString(this bool val, string true_str, string false_str)
        {
            return val ? true_str : false_str;
        }
        public static string AppendDir(this string dir, string appenDir)
        {
            return (dir + "//" + appenDir).Replace("////", "//");
        }
    }
}